﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yunwei.DAL;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.BizLogic
{
    public class RestaurantIngredientService : BaseService<RestaurantIngredient>, IRestaurantIngredientService
    {
        public RestaurantIngredientService() :base(RepositoryFactory.RestaurantIngredientRepository)
        {
        }

        public RestaurantIngredient FindByIngredient(int ingredient_Id, int restaurant_Id)
        {
            if (ingredient_Id == 0 || restaurant_Id == 0) return null;

            IQueryable<RestaurantIngredient> ri = CurrentRepository.Entities;
            if (ingredient_Id > 0) ri = ri.Where(cm => cm.Ingredient_Id == ingredient_Id);
            if (restaurant_Id > 0) ri = ri.Where(s => s.Restaurant_Id == restaurant_Id);

            return ri.FirstOrDefault();
        }

        public IQueryable<RestaurantIngredient> FindList(int number, string name, int restaurant_Id, int orderCode)
        {
            return FindPageList(out number, 1, number, name, restaurant_Id, orderCode);
        }

        public IQueryable<RestaurantIngredient> FindPageList(out int totalRecord, int pageIndex, int pageSize, string name, int restaurant_Id, int order)
        {
            IQueryable<RestaurantIngredient> ri = CurrentRepository.Entities;
            if (!string.IsNullOrEmpty(name)) ri = ri.Where(cm => cm.Name.Contains(name));
            if (restaurant_Id > 0) ri = ri.Where(s => s.Restaurant_Id == restaurant_Id);

            ri = Order(ri, order);
            totalRecord = ri.Count();

            return PageList(ri, pageIndex, pageSize).AsQueryable();
        }

        public IQueryable<RestaurantIngredient> Order(IQueryable<RestaurantIngredient> entitys, int orderCode)
        {
            switch (orderCode)
            {
                //默认排序
                default:
                    entitys = entitys.OrderByDescending(cm => cm.Name);
                    break;
            }
            return entitys;
        }

    }
}
